package com.api.smoke.group;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.api.test.VoidContextLoader;
import com.api.util.AssertUtil;
import com.api.util.DataUtil;
import com.api.util.GenericUtils.GenericType;
import com.api.util.repo.DtoFactory;
import com.api.util.repo.impl.DomainUser;
import com.zyncro.api.IGroupApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.GroupApi;
import com.zyncro.api.model.GroupApiType;
import com.zyncro.api.model.GroupFilterApiType;
import com.zyncro.api.model.GroupOrderFieldFilterApiType;
import com.zyncro.api.model.OrderFilterApiType;
import com.zyncro.api.model.PagedItemsResultApi;

@ContextConfiguration(loader = VoidContextLoader.class)
public class GetGroupsSmoke extends AbstractApiTest {

	private PagedItemsResultApi<GroupApi> expectedResults;

	@BeforeClass
	protected void setUp() throws Exception {

		final String domainName = this.getClass().getSimpleName() + "2";
		DomainUser mainAdmin = getRepo().getMainUser(domainName);

		getApi().login(mainAdmin.getAppId(), mainAdmin.getPassword());

		String fileName = getRepo().getPropertyDto(domainName + "-FileGroup", new DtoFactory<String>() {

			public String createDto() {

				String env = System.getProperty("env");

				String fileName = String.format("./json/%s/%s.json", env, domainName);

				try {
					getApi().getService(IGroupApiService.class).createGroup("name", "decriptione", false, true, false, true, true, false,
							true, null);
					getApi().getService(IGroupApiService.class).createGroup("name1", null, false, true, false, true, true, false, true,
							null);
					getApi().getService(IGroupApiService.class).createGroup("name2", "decriptione", true, true, false, false, true, false,
							true, null);
					getApi().getService(IGroupApiService.class).createGroup("name3", "decriptione", false, true, false, true, true, false,
							true, null);

					PagedItemsResultApi<GroupApi> result = getApi().getService(IGroupApiService.class).getGroups(0, 0, null, null, null,
							null, null);

					DataUtil.saveDtoToFile(result, fileName);

				} catch (Exception e) {

					throw new IllegalStateException("Exception creating GetDepartments test data", e);
				}

				return fileName;
			}
		});

		this.expectedResults = DataUtil.getDtoFromFile(fileName, new GenericType<PagedItemsResultApi<GroupApi>>() {
		});
	}

	@Test
	public void testGetAllGroups() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<GroupApiType> groupTypes = null;
		GroupFilterApiType groupFilter = null;
		String startsWith = null;

		GroupOrderFieldFilterApiType orderFilterField = null;
		OrderFilterApiType orderFilterType = null;

		PagedItemsResultApi<GroupApi> result = getApi().getService(IGroupApiService.class).getGroups(pageNumber, itemsPerPage, groupTypes,
				groupFilter, startsWith, orderFilterField, orderFilterType);

		Assert.assertEquals(result.getPageNumber(), 1, "pageNumber");
		Assert.assertEquals(result.getTotalItems(), expectedResults.getTotalItems());

		Collection<GroupApi> resultElements = result.getElements();
		Collection<GroupApi> expectedElements = expectedResults.getElements();

		Iterator<GroupApi> it = expectedElements.iterator();
		for (GroupApi groupApi : resultElements) {
			GroupApi expectedGroup = it.next();
			AssertUtil.assertDTOEquals(groupApi, expectedGroup);
		}
	}
}
